//
//  ZXDestinationViewViewController.m
//  XiMaFM
//
//  Created by fanzhuxian on 2017/9/6.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "ZXDestinationViewViewController.h"

#import <UIImageView+AFNetworking.h>

#import "ZXDestinationTableViewCell.h"
#import "ZXTracksViewModel.h"

#import "CXRefreshStateHeader.h"

@interface ZXDestinationViewViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableView;

@property (nonatomic, strong) ZXTracksViewModel * destinationViewModel;

@property (nonatomic, assign) BOOL isAsc;

@end

@implementation ZXDestinationViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.tableView.mj_header beginRefreshing];
//    [self tableView];

}

- (instancetype)initWithAlumId:(NSInteger)almId title:(NSString *)oTitle{

    if (self = [super init]) {
        _albumId = almId;
        _oTitle = oTitle;
    }
    return self;
}

// 视图即将消失
- (void)viewWillDisappear:(BOOL)animated {

    self.navigationController.navigationBar.hidden = NO;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.destinationViewModel.rowNumber;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ZXDestinationTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"DestinationCell"];
    
    [cell.coverImgView setImageWithURL:[self.destinationViewModel coverURLForRow:indexPath.row] placeholderImage:[UIImage imageNamed:@"album_cover_bg"]];
    cell.titleLabel.text = [self.destinationViewModel titleForRow:indexPath.row];
    cell.timeLabel.text = [self.destinationViewModel updateTimeForRow:indexPath.row];
    [cell.playButton setTitle:[self.destinationViewModel playsCountForRow:indexPath.row] forState:UIControlStateNormal];
    [cell.durationButton setTitle:[self.destinationViewModel playTimeForRow:indexPath.row] forState:UIControlStateNormal];
    [cell.commetnButton setTitle:[self.destinationViewModel commentCountForRow:indexPath.row] forState:UIControlStateNormal];
    
    cell.backgroundColor = [UIColor colorWithRed:239 / 255.0  green:243 / 255.0  blue:247 / 255.0 alpha:1];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"点击");
}

#pragma mark - 懒加载
- (ZXTracksViewModel *)destinationViewModel {
    
    if (!_destinationViewModel) {
        _destinationViewModel = [[ZXTracksViewModel alloc] initWithAlbumId:_albumId title:_oTitle isAsc:!_isAsc];
    }
    return _destinationViewModel;
}

- (UITableView *)tableView {

    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT) style:UITableViewStylePlain];
        [self.view addSubview:_tableView];
        
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:@"ZXDestinationTableViewCell" bundle:nil] forCellReuseIdentifier:@"DestinationCell"];
        
        // 上拉刷新
        _tableView.mj_header = [CXRefreshStateHeader headerWithRefreshingBlock:^{
            [self.destinationViewModel getDataCompletionHandle:^(NSError *error) {
                [self.tableView.mj_header endRefreshing];
                [self.tableView reloadData];
            }];
        }];
        _tableView.rowHeight = 90;
    }
    return _tableView;
}


@end
